home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE20 / CLINIC / Directio / GIVEIO / GIVEIO.C next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  5.6 KB  |  169 lines

  1. /*********************************************************************
  2.  
  3. Author:     Dale Roberts
  4. Date:       8/30/95
  5. Program:    GIVEIO.SYS
  6. Compile:    Use DDK BUILD facility
  7.  
  8. Purpose:    Give direct port I/O access to a user mode process.
  9.  
  10. *********************************************************************/
  11. #include <ntddk.h>
  12.  
  13. /*
  14.  *  The name of our device driver.
  15.  */
  16. #define DEVICE_NAME_STRING    L"giveio"
  17.  
  18. /*
  19.  *  This is the "structure" of the IOPM.  It is just a simple
  20.  * character array of length 0x2000.
  21.  *
  22.  *  This holds 8K * 8 bits -> 64K bits of the IOPM, which maps the
  23.  * entire 64K I/O space of the x86 processor.  Any 0 bits will give
  24.  * access to the corresponding port for user mode processes.  Any 1
  25.  * bits will disallow I/O access to the corresponding port.
  26.  */
  27. #define    IOPM_SIZE    0x2000
  28. typedef UCHAR IOPM[IOPM_SIZE];
  29.  
  30. /*
  31.  *  This will hold simply an array of 0's which will be copied
  32.  * into our actual IOPM in the TSS by Ke386SetIoAccessMap().
  33.  * The memory is allocated at driver load time.
  34.  */
  35. IOPM *IOPM_local = 0;
  36.  
  37. /*
  38.  *  These are the two undocumented calls that we will use to give
  39.  * the calling process I/O access.
  40.  *
  41.  *  Ke386IoSetAccessMap() copies the passed map to the TSS.
  42.  *
  43.  *  Ke386IoSetAccessProcess() adjusts the IOPM offset pointer so that
  44.  * the newly copied map is actually used.  Otherwise, the IOPM offset
  45.  * points beyond the end of the TSS segment limit, causing any I/O
  46.  * access by the user mode process to generate an exception.
  47.  */
  48. void Ke386SetIoAccessMap(int, IOPM *);
  49. void Ke386QueryIoAccessMap(int, IOPM *);
  50. void Ke386IoSetAccessProcess(PEPROCESS, int);
  51.  
  52. /*********************************************************************
  53.   Release any allocated objects.
  54. *********************************************************************/
  55. VOID GiveioUnload(IN PDRIVER_OBJECT DriverObject)
  56. {
  57.     WCHAR DOSNameBuffer[] = L"\\DosDevices\\" DEVICE_NAME_STRING;
  58.     UNICODE_STRING uniDOSString;
  59.  
  60.     if(IOPM_local)
  61.         MmFreeNonCachedMemory(IOPM_local, sizeof(IOPM));
  62.  
  63.     RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);
  64.     IoDeleteSymbolicLink (&uniDOSString);
  65.     IoDeleteDevice(DriverObject->DeviceObject);
  66. }
  67.  
  68. /*********************************************************************
  69.   Set the IOPM (I/O permission map) of the calling process so that it
  70. is given full I/O access.  Our IOPM_local[] array is all zeros, so
  71. the IOPM will be all zeros.  If OnFlag is 1, the process is given I/O
  72. access.  If it is 0, access is removed.
  73. *********************************************************************/
  74. VOID SetIOPermissionMap(int OnFlag)
  75. {
  76.     Ke386IoSetAccessProcess(PsGetCurrentProcess(), OnFlag);
  77.     Ke386SetIoAccessMap(1, IOPM_local);
  78. }
  79.  
  80. void GiveIO(void)
  81. {
  82.     SetIOPermissionMap(1);
  83. }
  84.  
  85. /*********************************************************************
  86.   Service handler for a CreateFile() user mode call.
  87.  
  88.   This routine is entered in the driver object function call table by
  89. the DriverEntry() routine.  When the user mode application calls
  90. CreateFile(), this routine gets called while still in the context of
  91. the user mode application, but with the CPL (the processor's Current
  92. Privelege Level) set to 0.  This allows us to do kernel mode
  93. operations.  GiveIO() is called to give the calling process I/O
  94. access.  All the user mode application needs do to obtain I/O access
  95. is open this device with CreateFile().  No other operations are
  96. required.
  97. *********************************************************************/
  98. NTSTATUS GiveioCreateDispatch(
  99.     IN  PDEVICE_OBJECT  DeviceObject,
  100.     IN  PIRP            Irp
  101.     )
  102. {
  103.     GiveIO();            // give the calling process I/O access
  104.  
  105.     Irp->IoStatus.Information = 0;
  106.     Irp->IoStatus.Status = STATUS_SUCCESS;
  107.     IoCompleteRequest(Irp, IO_NO_INCREMENT);
  108.     return STATUS_SUCCESS;
  109. }
  110.  
  111. /*********************************************************************
  112.   Driver Entry routine.
  113.  
  114.   This routine is called only once after the driver is initially
  115. loaded into memory.  It allocates everything necessary for the
  116. driver's operation.  In our case, it allocates memory for our IOPM
  117. array, and creates a device which user mode applications can open.
  118. It also creates a symbolic link to the device driver.  This allows
  119. a user mode application to access our driver using the \\.\giveio
  120. notation.
  121. *********************************************************************/
  122. NTSTATUS DriverEntry(
  123.     IN PDRIVER_OBJECT DriverObject,
  124.     IN PUNICODE_STRING RegistryPath
  125.     )
  126. {
  127.     PDEVICE_OBJECT deviceObject;
  128.     NTSTATUS status;
  129.     WCHAR NameBuffer[] = L"\\Device\\" DEVICE_NAME_STRING;
  130.     WCHAR DOSNameBuffer[] = L"\\DosDevices\\" DEVICE_NAME_STRING;
  131.     UNICODE_STRING uniNameString, uniDOSString;
  132.  
  133.     //
  134.     //  Allocate a buffer for the local IOPM and zero it.
  135.     //
  136.     IOPM_local = MmAllocateNonCachedMemory(sizeof(IOPM));
  137.     if(IOPM_local == 0)
  138.         return STATUS_INSUFFICIENT_RESOURCES;
  139.     RtlZeroMemory(IOPM_local, sizeof(IOPM));
  140.  
  141.     //
  142.     //  Set up device driver name and device object.
  143.     //
  144.     RtlInitUnicodeString(&uniNameString, NameBuffer);
  145.     RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);
  146.  
  147.     status = IoCreateDevice(DriverObject, 0,
  148.                     &uniNameString,
  149.                     FILE_DEVICE_UNKNOWN,
  150.                     0, FALSE, &deviceObject);
  151.  
  152.     if(!NT_SUCCESS(status))
  153.         return status;
  154.  
  155.     status = IoCreateSymbolicLink (&uniDOSString, &uniNameString);
  156.  
  157.     if (!NT_SUCCESS(status))
  158.         return status;
  159.  
  160.     //
  161.     //  Initialize the Driver Object with driver's entry points.
  162.     // All we require are the Create and Unload operations.
  163.     //
  164.     DriverObject->MajorFunction[IRP_MJ_CREATE] = GiveioCreateDispatch;
  165.     DriverObject->DriverUnload = GiveioUnload;
  166.     return STATUS_SUCCESS;
  167. }
  168.  
  169.